import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:kkhousekeep/page/home/home_main_page.dart';
import 'package:kkhousekeep/page/login/login.dart';
import 'package:kkhousekeep/page/login/login_controller.dart';
import 'package:kkhousekeep/tools/shared_preferences.dart';

void main() async {
  ///强制竖屏
  WidgetsFlutterBinding.ensureInitialized();
  String hadToken = await getSp().getAccessToken();
  bool hadCheckPass = await getSp().getRoomCheckPass();
  UserType userType = await getSp().getUserType();
  bool hadLogin = hadToken.isNotEmpty;
  runApp(
    MyApp(
      hadLogin: hadLogin,
      hadCheckPass: hadCheckPass,
      userType: userType,
    ),
  );
}

class MyApp extends StatelessWidget {
  final bool hadLogin;
  final bool hadCheckPass;
  final UserType userType;

  const MyApp({
    super.key,
    required this.hadLogin,
    required this.hadCheckPass,
    required this.userType,
  });

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(750, 1624),
      minTextAdapt: true,
      splitScreenMode: true,
      builder: (BuildContext context, Widget? child) {
        return GetMaterialApp(
          title: 'Flutter Demo',
          debugShowCheckedModeBanner: false,
          //替换英文为中文
          locale: const Locale('zh', 'CN'),
          supportedLocales: const [Locale('zh', 'CN')],
          localizationsDelegates: const [
            GlobalMaterialLocalizations.delegate,
            GlobalCupertinoLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate
          ],
          navigatorObservers: [FlutterSmartDialog.observer],
          builder: FlutterSmartDialog.init(),
          theme: ThemeData(
            primarySwatch: Colors.blue,
            appBarTheme: const AppBarTheme(
              systemOverlayStyle: SystemUiOverlayStyle(
                statusBarColor: Colors.transparent,
                statusBarIconBrightness: Brightness.dark,
                statusBarBrightness: Brightness.light,
              ),
            ),
          ),
          home: hadLogin
              ? HomeMainPage(
                  index: hadCheckPass ? 0 : 1,
                  userType: userType,
                )
              : const LoginPage(),
        );
      },
    );
  }
}
